<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部区域 -->
        <TreeTools
          @openDialog="openDialog"
          :treeData="company"
          :isRoot="true"
        ></TreeTools>
        <!-- 树形组件区域 -->
        <!-- ⭐4.渲染 -->
        <el-tree :data="data" :props="{ label: 'name', children: 'children' }">
          <!-- 再次复用头部组件 -->
          <!-- ⭐slot-scope="{ node, data }" el-tree带的作用域插槽方法获取el-tree组件内部数据 -->
          <TreeTools
            @editDialog="editDialog"
            @openDialog="openDialog"
            slot-scope="{ data }"
            :treeData="data"
            @updateList="iniData"
          ></TreeTools>
        </el-tree>
      </el-card>
    </div>
    <!--⭐新增修改弹窗  -->
    <!-- <el-dialog title="新增/修改" :visible.sync="dialogShow">弹窗内荣</el-dialog> -->
    <AddDept ref="addDept" @updataList="iniData"></AddDept>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartmentListApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '传值教育', manager: '负责人', id: '' },
      // ⭐1.定义变量 存储数据
      data: []
      // 弹出框控制变量 =>转移到组件
      // dialogShow: false
    }
  },
  created() {
    // ⭐3.生命周期调用
    this.iniData()
  },
  methods: {
    // ⭐2.获取数据
    async iniData() {
      // 2.1发请求
      const { companyName, depts } = await getDepartmentListApi()
      // console.log(companyName, depts)
      // 2.2数据赋值
      this.company.name = companyName
      // this.data = depts
      this.data = tranListToTreeData(depts, '')
    },
    // 打开弹窗事件
    openDialog(val) {
      // this.dialogShow = true
      // ⭐⭐$refs.addDept里可以拿到dialogShow属性（$refs.可以用来获取组件实例和dom实例）
      this.$refs.addDept.dialogShow = true
      // 传树形图上级id用于校验
      this.$refs.addDept.formData.pid = val
    },
    // 点击编辑事件
    editDialog(val) {
      this.$refs.addDept.getFormDataInfo(val)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
